/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amse.grigory.dchess.view;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.amse.grigory.dchess.io.*;
import org.amse.grigory.dchess.player.*;
import org.amse.grigory.dchess.rules.Rules;

/**
 *
 * @author grigory
 */
public class NewGameDialog extends JDialog {

    private static String[] myTimeList = {"5 min", "15 min", "30 min", "1 hour", "2 hour", "unlimited", "custom..."};
    private static int[] myTimeValue = {300, 900, 1800, 3600, 7200, 315360000};
    private int myState = 0;
    private boolean myExit = false;
    private JComboBox myComboBoxRules,  myComboBoxTheme,  myComboBoxWhite,  myComboBoxBlack;
    private ConfigLoader myConfig;
    private JSpinner mySpinner;
    private JComboBox myComboBoxTime;
    private int myTime = 300;

    private NewGameDialog() {
    }

    public NewGameDialog(final DChessFrame frame) {
        super(frame, true);
        setSize(300, 350);
        setLocation(frame.getLocation().x + 100, frame.getLocation().y + 100);
        setResizable(false);
        Container c = getContentPane();
        myConfig = frame.getConfig();
        Dimension dim = new Dimension(25, 200);
        createComboBoxes();
        SpinnerModel numbers = new SpinnerNumberModel(300, 10, 300000000, 1);
        mySpinner = new JSpinner(numbers);
        mySpinner.setMaximumSize(dim);
        myComboBoxRules.setAlignmentX(LEFT_ALIGNMENT);
        myComboBoxTheme.setAlignmentX(LEFT_ALIGNMENT);
        mySpinner.setAlignmentX(LEFT_ALIGNMENT);


        //Panels layouts
        JPanel panelGame = createPanelGame();
        JPanel panelPlayers = createPanelPlayers();
        JPanel panelButtons = createPanelButtons();

        c.setLayout(new FlowLayout());
        panelGame.setPreferredSize(new Dimension(280, 150));
        panelPlayers.setPreferredSize(new Dimension(280, 100));
        panelButtons.setPreferredSize(new Dimension(280, 100));
        //c.setLayout(new GridLayout(3, 1));
        c.add(panelGame);
        c.add(panelPlayers);
        c.add(panelButtons);

        setSize(300, 350);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new DChessListener(frame));
        setVisible(true);

    }

    private JPanel createPanelButtons() {

        // buttons
        JButton buttonOK = new JButton("OK");
        buttonOK.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                myState = 1;
                NewGameDialog.this.dispose();
            }
        });
        JButton buttonCancel = new JButton("Cancel");
        buttonCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                NewGameDialog.this.dispose();
            }
        });



        buttonCancel.setAlignmentX(LEFT_ALIGNMENT);
        buttonOK.setAlignmentX(LEFT_ALIGNMENT);
        Dimension dim = new Dimension(75, 25);
        buttonOK.setSize(dim);
        buttonOK.setPreferredSize(dim);
        buttonOK.setMaximumSize(dim);
        buttonOK.setMinimumSize(dim);
        buttonCancel.setSize(dim);
        buttonCancel.setPreferredSize(dim);
        buttonCancel.setMaximumSize(dim);
        buttonCancel.setMinimumSize(dim);

        //panels
        JPanel panelButtons = new JPanel();
        JPanel panelBut = new JPanel();
        panelButtons.setLayout(new GridLayout(2, 1));
        panelBut.setLayout(new BoxLayout(panelBut, BoxLayout.X_AXIS));

        panelButtons.add(panelBut);
        panelBut.add(Box.createHorizontalGlue());
        panelBut.add(buttonOK);
        panelBut.add(Box.createHorizontalGlue());
        panelBut.add(buttonCancel);
        panelBut.add(Box.createHorizontalGlue());

        return panelButtons;
    }

    private JPanel createPanelPlayers() {
        JPanel panelPlayers = new JPanel();
        JPanel panelWhite = new JPanel();
        JPanel panelBLack = new JPanel();
        JLabel l3 = new JLabel("White:");
        JLabel l4 = new JLabel("Black:");

        panelPlayers.setBorder(BorderFactory.createTitledBorder("Players"));
        panelPlayers.setLayout(new GridLayout(2, 1));

        panelBLack.setLayout(new BoxLayout(panelBLack, BoxLayout.X_AXIS));
        panelWhite.setLayout(new BoxLayout(panelWhite, BoxLayout.X_AXIS));

        panelWhite.add(l3);
        panelWhite.add(Box.createHorizontalGlue());
        panelWhite.add(myComboBoxWhite);
        panelBLack.add(l4);
        panelBLack.add(Box.createHorizontalGlue());
        panelBLack.add(myComboBoxBlack);

        panelPlayers.add(panelWhite);
        panelPlayers.add(panelBLack);

        return panelPlayers;
    }

    private JPanel createPanelGame() {
        JPanel panelGame = new JPanel();
        JPanel panelRules = createPanelRules();
        JPanel PTheme = new JPanel();
        JPanel PTime = new JPanel();
        JLabel l2 = new JLabel("Theme:");
        JLabel l5 = new JLabel("Time:");

        PTheme.setLayout(new BoxLayout(PTheme, BoxLayout.X_AXIS));
        PTime.setLayout(new BoxLayout(PTime, BoxLayout.X_AXIS));

        panelGame.setBorder(BorderFactory.createTitledBorder("Game"));
        panelGame.setLayout(new GridLayout(3, 1));

        panelGame.add(panelRules);
        panelGame.add(PTheme);
        panelGame.add(PTime);

        PTheme.add(l2);
        PTheme.add(Box.createHorizontalGlue());
        PTheme.add(myComboBoxTheme);
        PTime.add(l5);
        PTime.add(Box.createHorizontalGlue());
        PTime.add(myComboBoxTime);
        return panelGame;
    }

    private JPanel createPanelRules() {
        JPanel panelRules = new JPanel();
        panelRules.setLayout(new BoxLayout(panelRules, BoxLayout.X_AXIS));
        JLabel l1 = new JLabel("Rules:");
        panelRules.add(l1);
        panelRules.add(Box.createHorizontalGlue());
        panelRules.add(myComboBoxRules);
        return panelRules;
    }

    private void createComboBoxes() {
        String[] rname, tname;
        // first combobox
        rname = new String[myConfig.getRulesCount()];
        for (int i = 0; i < myConfig.getRulesCount(); i++) {
            rname[i] = myConfig.getRulesName(i);
        }
        myComboBoxRules = new JComboBox(rname);
        Dimension dim = new Dimension(200, 25);
        myComboBoxRules.setPreferredSize(dim);
        myComboBoxRules.setMaximumSize(dim);

        // second combobox
        tname = new String[myConfig.getThemeCount()];
        for (int i = 0; i < myConfig.getThemeCount(); i++) {
            tname[i] = myConfig.getThemeName(i);
        }
        myComboBoxTheme = new JComboBox(tname);
        myComboBoxTheme.setPreferredSize(dim);
        myComboBoxTheme.setMaximumSize(dim);

        myComboBoxTime = new JComboBox(myTimeList);
        myComboBoxTime.setMaximumSize(dim);
        myComboBoxTime.setPreferredSize(dim);
        myComboBoxTime.addActionListener(new ListListener());

        String players[] = {"Local","AI"};

        //two more combox
        myComboBoxWhite = new JComboBox(players);
        myComboBoxWhite.setPreferredSize(dim);
        myComboBoxWhite.setMaximumSize(dim);
        myComboBoxBlack = new JComboBox(players);
        myComboBoxBlack.setPreferredSize(dim);
        myComboBoxBlack.setMaximumSize(dim);
    }

    private class ListListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (myComboBoxTime.getSelectedItem().equals("custom...")) {
                int ans = 300;
                while (true) {
                    String res = JOptionPane.showInputDialog("Enter time in seconds");
                    if (res == null) {
                        myComboBoxTime.setSelectedIndex(0);
                        myTime = myTimeValue[0];
                        return;
                    }
                    try {
                        ans = Integer.valueOf(res);
                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(rootPane, "Wrong format!", "Error", JOptionPane.ERROR_MESSAGE);
                        continue;
                    }
                    break;
                }
                myTime = ans;
            }
        }
    }

    private void getTime(int i) {
        String s = myTimeList[myComboBoxTime.getSelectedIndex()];
        if (s.equals("custom...")) {
            return;
        }
        myTime = myTimeValue[myComboBoxTime.getSelectedIndex()];
    }

    private class DChessListener extends WindowAdapter {

        private DChessFrame myFrame;

        public DChessListener(DChessFrame frame) {
            myFrame = frame;
        }

        @Override
        public void windowClosing(WindowEvent e) {
            NewGameDialog.this.dispose();
        }

        @Override
        public void windowClosed(WindowEvent e) {
            if (!myExit) {
                myFrame.setFocusableWindowState(true);
                myFrame.setEnabled(true);
                if (myState == 1) {
                    int i = myComboBoxRules.getSelectedIndex();
                    int j = myComboBoxTheme.getSelectedIndex();
                    getTime(myComboBoxTime.getSelectedIndex());
                    int time = myTime;
                    myFrame.setRules(myConfig.getRules(i));
                    myFrame.setTheme(myConfig.getTheme(j));
                    myFrame.setTime(time);
                    IPlayer pw = null;
                    String sw = (String) myComboBoxWhite.getSelectedItem();
                    if (sw.equals("Local")) {
                        pw = new LocalPlayer();
                    }
                    if (sw.equals("AI")) {
                        pw = new AIPlayer(3, myFrame.getModel());
                        pw.setColor(Rules.COLOR_WHITE);
                    }
                    IPlayer pb = null;
                    String sb = (String) myComboBoxBlack.getSelectedItem();
                    if (sb.equals("Local")) {
                        pb = new LocalPlayer();
                    }
                    if (sb.equals("AI")) {
                        pb = new AIPlayer(3, myFrame.getModel());
                        pb.setColor(Rules.COLOR_BLACK);
                    }
                    GameControler gamec = new GameControler(myFrame.getModel(), pw, pb);
                    myFrame.startGame(gamec);
                    myExit = true;
                }
            }
        }
    }
}
